<html>
<head><meta charset="utf-8"><title>generate function machine code · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html">generate function machine code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202428288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202428288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202428288">(Jun 30 2020 at 12:35)</a>:</h4>
<p>I want that same thing, but for <code>asm!</code> and <code>global_asm!</code>. That is, declaring whole new compilation units in <code>const-eval</code> with proper support for lto flags and everything else that rustc already supports.</p>



<a name="202428349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202428349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202428349">(Jun 30 2020 at 12:35)</a>:</h4>
<p>Or for declaring functions bodies, it wouldn't be much different.</p>



<a name="202428418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202428418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202428418">(Jun 30 2020 at 12:36)</a>:</h4>
<p>so you basically want to create the binary code for a function via CTFE and have the compiler emit that into the final binary?</p>



<a name="202428491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202428491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202428491">(Jun 30 2020 at 12:37)</a>:</h4>
<p>I think you can already do that by using a <code>static foo: [u8; N] = ...;</code> and creating an <code>extern { fn foo(); }</code> and invoking that</p>



<a name="202428517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202428517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202428517">(Jun 30 2020 at 12:37)</a>:</h4>
<p>but yea, having first class support for that could be neat</p>



<a name="202428798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202428798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202428798">(Jun 30 2020 at 12:40)</a>:</h4>
<p>I can do that, if I assemble it all the way myself. Which means such a proc-macro that assembles it can not benefit from the compiler infrastructure that already exists. (I've tested that and lto didn't appear to work). It also means that target architecture is finicky and using the values of other constants is incredibly hard to do properly.</p>



<a name="202429027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429027">(Jun 30 2020 at 12:43)</a>:</h4>
<p>The static's initializer has access to everything in the compiler that CTFE normally has access to, so if you do your machine code generation in CTFE instead of in a proc macro, then that should work, right?</p>



<a name="202429125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429125">(Jun 30 2020 at 12:44)</a>:</h4>
<p>Of course with the limited state of CTFE right now that's likely unfeasible, but I don't see anything in the "set of things that we all agree on shoudl be in CTFE in some form" that would be missing for that</p>



<a name="202429168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429168">(Jun 30 2020 at 12:44)</a>:</h4>
<p>Exactly, mapping the <code>asm!</code> text input into code that correctly evaluates to your byte-encoded machine code is quite hard to do if some bit fields insaid byte code are supposed to be constructed during CTFE.</p>



<a name="202429223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429223">(Jun 30 2020 at 12:45)</a>:</h4>
<p>And it doesn't solve the relocation problem. I simply _can not_ use a function address if it's not a constant.</p>



<a name="202429333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429333">(Jun 30 2020 at 12:46)</a>:</h4>
<p>I'd like to tell the compiler: 'Hey, these 4 bytes here are going to be a relocation and you need to resolve it against something that gets linked later on'.</p>



<a name="202429356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429356">(Jun 30 2020 at 12:46)</a>:</h4>
<p>yea, that's how CTFE pointers work :D</p>



<a name="202429398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429398">(Jun 30 2020 at 12:46)</a>:</h4>
<p>we delay the actual resolving all the way to llvm</p>



<a name="202429423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429423">(Jun 30 2020 at 12:47)</a>:</h4>
<p>You're not allowed to embed the bytes of the pointers individually, afai,.</p>



<a name="202429447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429447">(Jun 30 2020 at 12:47)</a>:</h4>
<p>you can already do all this with enough feature gates and lots of unsafe</p>



<a name="202429465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429465">(Jun 30 2020 at 12:47)</a>:</h4>
<p>sure, but just write all of them in one go</p>



<a name="202429488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429488">(Jun 30 2020 at 12:47)</a>:</h4>
<p>but that's not an inherent blocker either, we just don't have the impl for it</p>



<a name="202429574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429574">(Jun 30 2020 at 12:48)</a>:</h4>
<p>.. Sounds nice. Best solution i've found until now would be to generate a struct, not <code>[u8; N]</code>  so that everything I want to embed from the environment during CTFE stays in its respective type and is 'nicely' evaluated.</p>



<a name="202429611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429611">(Jun 30 2020 at 12:48)</a>:</h4>
<p>Mark it repr(C) and it's as good a bytecode body as any other byte array.</p>



<a name="202429635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429635">(Jun 30 2020 at 12:48)</a>:</h4>
<p>Yea, you're likely gonna need a union or something else that allows you to store pointers in integers</p>



<a name="202429654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429654">(Jun 30 2020 at 12:49)</a>:</h4>
<p>you can't have a <code>u8</code> that is actually a relocation</p>



<a name="202429731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429731">(Jun 30 2020 at 12:49)</a>:</h4>
<p>but you can have a <code>MaybeInitialized&lt;[u8; N]&gt;</code> and write your pointer by getting a pointer to the destination and casting that pointer liberally to whatever works</p>



<a name="202429784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429784">(Jun 30 2020 at 12:50)</a>:</h4>
<p>Which means no short jumps to other symbols generated via that technique.</p>



<a name="202429813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429813">(Jun 30 2020 at 12:50)</a>:</h4>
<p>uh</p>



<a name="202429826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429826">(Jun 30 2020 at 12:50)</a>:</h4>
<p>Now that's a different issue entirely ;)</p>



<a name="202429882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429882">(Jun 30 2020 at 12:51)</a>:</h4>
<p>you are machinegunning out advanced feature requests here at quite  a rate (this is not a complaint, just mentioning that we're talking about many different features here)</p>



<a name="202429932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429932">(Jun 30 2020 at 12:51)</a>:</h4>
<p>I'm not sure if llvm can do symbol subtraction in order to create relative jumps</p>



<a name="202429935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202429935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202429935">(Jun 30 2020 at 12:51)</a>:</h4>
<p>Oh right, well simpler example would be that I can't change the byte order of a function pointer during CTFE with purpose of embedding it</p>



<a name="202430099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202430099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202430099">(Jun 30 2020 at 12:53)</a>:</h4>
<p>The symbol subtraction would need to happen after the symbols are one linked TU. Maybe ctfe all the way through llvm? Maybe they can offer us a neat wasm vm in the linker?</p>



<a name="202430225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202430225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202430225">(Jun 30 2020 at 12:54)</a>:</h4>
<p>yea, but as mentioned above, this is actually a feature we're thinking about adding to the engine. Kinda related to <a href="https://github.com/rust-lang/rust/issues/69488">https://github.com/rust-lang/rust/issues/69488</a></p>



<a name="202430306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202430306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202430306">(Jun 30 2020 at 12:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="229913">HeroicKatora</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/generate.20function.20machine.20code/near/202430099">said</a>:</p>
<blockquote>
<p>The symbol subtraction would need to happen after the symbols are one linked TU. Maybe ctfe all the way through llvm? Maybe they can offer us a neat wasm vm in the linker?</p>
</blockquote>
<p>No, CTFE can't run anymore that late. Basically it needs first class llvm support</p>



<a name="202430373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202430373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202430373">(Jun 30 2020 at 12:55)</a>:</h4>
<p>Hm, maybe linker scripts are powerful enough?</p>



<a name="202430393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/generate%20function%20machine%20code/near/202430393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/generate.20function.20machine.20code.html#202430393">(Jun 30 2020 at 12:56)</a>:</h4>
<p>possible</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>